﻿using CSharpTestProject.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharpTestProject.AdminFormFolder
{
    public partial class AddTeacherForm : Form
    {
        public _Sql sql;
        public AddTeacherForm()
        {
            InitializeComponent();
        }

        private void AddTeacherForm_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string course = textBox4.Text.Trim();
            if (!course.EndsWith(";"))
            {
                course += ";";
            }
            string query = $"SELECT count(*) FROM usertable WHERE logname='{textBox2.Text}' AND yhlx='teacher'";
            string s1 = $"INSERT INTO teachers (teachername,logname,kcm,zgh) VALUES('{textBox1.Text}','{textBox2.Text}','{course}','{textBox5.Text}')";
            string s2 = $"INSERT INTO usertable (logname,password,yhlx) VALUES('{textBox2.Text}','{textBox3.Text}','teacher')";
            DataSet ds = sql.SqlFind(query);
            if (int.Parse(ds.Tables[0].Rows[0][0].ToString()) != 0)
            {
                MessageBox.Show("用户名重复！");
                return;
            }
            try
            {
                sql.ManageData(s1);
                sql.ManageData(s2);
                MessageBox.Show("提交成功！");
                return;
            }
            catch{
                MessageBox.Show("上传失败,请检查数据是否正确!");
                return;
            }
        }
    }
}
